/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package BD;

import Administrador.Interfaz.ConexionBD;
import java.sql.Connection;
import java.sql.*;
import java.util.ArrayList;
import java.util.Calendar;
import javax.swing.JOptionPane;

/**
 *
 * @author USUARIO
 */
public class GestorPersonal {
    
    private ConexionBD conexionBD;
    private  Connection con;
    private boolean existe;
    protected String empleado;
   
    public boolean existe()
    {
       return existe;
    }

    public GestorPersonal () 
    {
       conexionBD = new ConexionBD();
    }
 
    
   public void registrarEmpleado(String nCedula, String nombres, String apellidos, Calendar fechaNacimiento, String direccion, String telefono, String tipo,String tcontrasena) {
        
        try {
            con = conexionBD.conectar();
            PreparedStatement stmt = con.prepareStatement("insert into personal values( ?, ?, ?, ?, ?, ?, ?, ? );");
            stmt.setString(1,nCedula);
            stmt.setString(2, nombres);
            stmt.setString(3, apellidos);
            stmt.setDate(4, new java.sql.Date(fechaNacimiento.getTimeInMillis()));
            stmt.setString(5, direccion);
            stmt.setString(6, telefono);
            stmt.setString(7,tipo);
            stmt.setString(8, tcontrasena);
             
            int count= stmt.executeUpdate();
            
            if(count!=0){
                JOptionPane.showMessageDialog(null, "Empleado agregado");
            }else{
                JOptionPane.showMessageDialog(null, "No se pudo ingresar el empleado");
            }
            
            stmt.close();
            con.close();
        } catch (SQLException e) {
            System.err.println("Exception: " + e.getMessage());
        }
    }

   public void eliminarEmpleado(String cedula) {
        try {
            con = conexionBD.conectar();
            PreparedStatement stmt = con.prepareStatement("delete from personal where nCedula = ? ;");
            stmt.setString(1,cedula);  
             
            int count= stmt.executeUpdate();
            
            if(count!=0){
                JOptionPane.showMessageDialog(null, "Empleado Eliminado");
            }else{
                JOptionPane.showMessageDialog(null, "No se pudo eliminar el empleado");
            }
            
          
            stmt.close();
            con.close();
        } catch (SQLException e) {
            System.err.println("Exception: " + e.getMessage());
        }
    
    }

  public  void ModificarEmpleado(String cedula,String tel, String direccion) {
         int count=0;
            
         try {
            con = conexionBD.conectar();
            if(tel==null){
            PreparedStatement stmt = con.prepareStatement("update personal set direccion=? where nCedula=?;");
            stmt.setString(1,direccion);  
            stmt.setString(2,cedula);
              count= stmt.executeUpdate();
              stmt.close();
            } else{if(direccion==null){
            PreparedStatement stmt = con.prepareStatement("update personal set telefono=? where nCedula=?;");
            stmt.setString(1,tel);  
            stmt.setString(2,cedula);
              count= stmt.executeUpdate();
              stmt.close();
            }else{
            if(tel==null){
            PreparedStatement stmt = con.prepareStatement("update personal set direccion=?, telefono=? where nCedula=?;");
            stmt.setString(1,direccion);
            stmt.setString(2,tel);
            stmt.setString(3,cedula);
              count= stmt.executeUpdate();
              stmt.close();
            }
            }} 
           
            if(count!=0){
                JOptionPane.showMessageDialog(null, "Empleado Modificado");
            }else{
                JOptionPane.showMessageDialog(null, "No se pudo Modificar el empleado");
            }
            
          
            
            con.close();
        } catch (SQLException e) {
            System.err.println("Exception: " + e.getMessage());
        }
    }
 
   public  ArrayList<String> consultarEmpleado(String cedula) {
  
       ArrayList<String> datos= new ArrayList<String>();
              
        PreparedStatement stmt=null;
        ResultSet result=null;
        
        try{
            con= conexionBD.conectar();
            stmt= con.prepareStatement("select nombres,apellidos,fechaNacimiento,direccion,telefono from personal where nCedula= ?;");
            stmt.setString(1,cedula);
            result= stmt.executeQuery();
             while (result.next()) {
            datos.add(result.getString(1));
            datos.add(result.getString(2));
            datos.add(result.getString(3));
            datos.add(result.getString(4));
            datos.add(result.getString(5));
             } 
            
        }catch(SQLException ex){
            
        }
        
        return datos;
        
   
   
   
   }

    
}
